package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.Product;
import cn.wolfcode.crm.mapper.ProductMapper;
import cn.wolfcode.crm.query.impl.ProductQuery;
import cn.wolfcode.crm.service.IProductService;
import cn.wolfcode.crm.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

@Service
public class ProductServiceImpl implements IProductService {
    @Autowired
    ProductMapper productMapper;

    public void delete(Long id) {
        productMapper.deleteByPrimaryKey(id);
    }

    public void saveOrUpdate(Product record) {
        if(record.getId()!=null){
        productMapper.updateByPrimaryKey(record);
        }else{
            productMapper.insert(record);
        }
    }

    public Product get(Long id) {
        return productMapper.selectByPrimaryKey(id);
    }

    public List<Product> getAll() {
        return productMapper.selectAll();
    }


    public PageResult query(ProductQuery qo) {
        int rows = productMapper.queryForCount(qo);
        if (rows == 0) {
            new PageResult(rows , Collections.EMPTY_LIST);
        }
        List<Product> result = productMapper.queryForList(qo);
        return new PageResult(rows,result);
    }

    @Override
    public List<Product> listAll(String organizationName, Boolean state) {
        return productMapper.listAll(organizationName,state);
    }
}
